﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuanLyNhaThuoc.Models;
using System.Text;
using QuanLyNhaThuoc.HelperClass;

namespace QuanLyNhaThuoc.Views.Shared
{
    public partial class Main : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Cache["MENU"] == null)
                {
                    StringBuilder sb = new StringBuilder();
                    PMOEntities PMOEn = new PMOEntities();
                    var query = PMOEn.MenuLoad(Session["USERID"].ToString(), "0");
                    foreach (var item in query.ToList())
                    {
                        var child = PMOEn.MenuLoad(Session["USERID"].ToString(), item.MenuID.ToString()).ToList();
                        if (child.Count != 0)
                        {
                            sb.Append("<li class=\"treeview\" id='" + item.ID + "'>");
                        }
                        else
                        {
                            sb.Append("<li>");
                        }

                        sb.Append("<a href='" + item.Link + "'>");
                        sb.Append("<i class='" + item.Icon + "'></i>");
                        sb.Append("<span id='" + item.ID + "'>");
                        sb.Append(item.MenuName);
                        sb.Append("</span>");
                        if (child.Count != 0)
                        {
                            sb.Append("<i class=\"fa fa-angle-left pull-right\"></i>");
                        }

                        sb.Append("</a>");

                        if (child.Count != 0)
                        {
                            sb.Append("<ul class=\"treeview-menu\" id='" + item.ParentID + "'>");
                        }
                        foreach (var itemChild in child)
                        {

                            sb.Append("<li id='" + itemChild.ID + "'><a href='" + itemChild.Link + "'><i class=\"fa fa-angle-double-right\"></i>");
                            sb.Append(itemChild.MenuName);
                            sb.Append("</a></li>");

                        }
                        if (child.Count != 0)
                        {
                            sb.Append("</ul>");
                        }
                        sb.Append("</li>");
                    }

                    litMenu.Text = sb.ToString();
                    Cache["MENU"] = sb.ToString();
                }
                else
                {
                    litMenu.Text = Cache["MENU"].ToString();
                }
            }
            catch (Exception ex)
            {
                Log.LogError(ex);
                Response.Redirect("~/Error/Index", false);
            }
        }
    }
}